package com.qc.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.common.StringUtil;
import com.qc.entity.SystemEntity;
import com.qc.service.SysTemService;

@Controller
public class LoginController {

    @Resource
    private SysTemService sysTemService;
    
    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping(value = "/toLogin", method = RequestMethod.GET)
    public ModelAndView toLogin() {
        ModelAndView mav = new ModelAndView("/login");
        return mav;
    }
    
    /**
     * 登录
     */
    @RequestMapping(value = "/toUserLogin", method = RequestMethod.POST)
    public @ResponseBody String toUserLogin(HttpServletRequest request,HttpServletResponse response) {
        
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        
        if (StringUtil.isNotEmpty(userName) && StringUtil.isNotEmpty(password)) {
            SystemEntity system = sysTemService.getSysTem(userName, password);
            if (system.getUserName() != null) {
                
                UserCookie cookie= new UserCookie();
                String ip = getIpAddr(request);
                String value=ip+"-&&&&"+userName+"-&&&&"+password;
                cookie.addCookie(response,"autouser",value);
                
                request.getSession().setAttribute("system", system);
                return "1";
            }else {
                return "2";
            }
        }else {
            return "3";
        }
    }
    
    /**
     * 退出登录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/logout.do", method = RequestMethod.GET)
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
        // 是当前Session失效
        
        Cookie cookie = new Cookie("autouser",null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        
        request.getSession().invalidate();
        
        ModelAndView mav = new ModelAndView("redirect:/toLogin.do");
        // 将用户请求转发到首页
        return mav;
    }
    
    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    
    
}
